import 'package:syria_sports/core/utils/validation_functions.dart';

import '../../../../widgets/app_bar/appbar_subtitle.dart';
import '../../../../widgets/app_bar/custom_app_bar.dart';
import '../../../../widgets/app_bar/outline_icon_button.dart';
import '../../../../widgets/custom_elevated_button.dart';
import '../../../../widgets/custom_text_form_field.dart';
import '../../exercises_page/models/exercise.dart';
import '../../../gyms/gyms_page/models/gym.dart';
import 'add_exercise_page_cubit.dart';
import 'package:flutter/material.dart';
import 'package:syria_sports/core/app_export.dart';

class AddExercisePageScreen extends StatelessWidget {
  final Gym? gym;
  final Exercise? exercise;
  const AddExercisePageScreen({Key? key, this.gym, this.exercise}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    double width = MediaQuery.of(context).size.width;

    return Scaffold(
      appBar: _buildAppBar(context),
      body: BlocProvider<AddExercisePageCubit>(
        create: (context) => AddExercisePageCubit()..init(gym , exercise),
        child: BlocConsumer<AddExercisePageCubit, AddExercisePageState>(
          listener: (context, state) {
            // TODO: implement listener
          },
          builder: (context, state) {
            ImageConstant;
            var cubit = AddExercisePageCubit.get(context);

            return SizedBox(
              height: height,
              width: width,
              child: Column(
                children: [
                  Expanded(
                    child: Form(
                        key: cubit.formKey,
                        child: SingleChildScrollView(
                          physics: const BouncingScrollPhysics(),
                          child: Container(
                              width: double.maxFinite,
                              padding:
                              EdgeInsets.only(left: 16.h, top: 32.v, right: 16.h),
                              child: Column(
                                children: [
                                  // Name
                                  CustomTextFormField(
                                    controller: cubit.nameController,
                                    focusNode:cubit.nameFocusNode,
                                    onFieldSubmitted: (value){
                                      FocusScope.of(context).unfocus();
                                      FocusScope.of(context).requestFocus(cubit.targetGroupFocusNode);
                                    },
                                    textInputAction: TextInputAction.next,
                                    hintText: "ادخل اسم التمرين",
                                    title: 'اسم التمرين',
                                    textInputType: TextInputType.name,
                                    prefix: Container(
                                      margin: EdgeInsets.fromLTRB(
                                        16.h, 12.v, 10.h, 12.v,
                                      ),
                                      child: CustomImageView(
                                        imagePath: ImageConstant.imgUser,
                                        height: 24.adaptSize,
                                        width: 24.adaptSize,
                                      ),
                                    ),
                                    prefixConstraints: BoxConstraints(maxHeight: 48.v),
                                    validator: (value) {
                                      if (value == null || value.isEmpty) {
                                        return 'هذه الخانة الزامية!';
                                      }
                                      return null;
                                    },
                                    contentPadding: EdgeInsets.only(
                                      top: 15.v,
                                      right: 30.h,
                                      bottom: 15.v,
                                    ),
                                  ),
                                  SizedBox(height: 10.v),
                                  // Target Group
                                  CustomTextFormField(
                                      controller: cubit.targetGroupController,
                                      focusNode:cubit.targetGroupFocusNode,
                                      onFieldSubmitted: (value){
                                        FocusScope.of(context).unfocus();
                                        FocusScope.of(context).requestFocus(cubit.fromTimeFocusNode);
                                      },
                                      hintText: "ادخل الفئة المستهدفة",
                                      title: 'الفئة المستهدفة',
                                      textInputAction: TextInputAction.next,
                                      textInputType: TextInputType.streetAddress,
                                      prefix: Container(
                                          margin: EdgeInsets.fromLTRB(
                                              16.h, 12.v, 10.h, 12.v),
                                          child: CustomImageView(
                                              imagePath: ImageConstant.icBarcode,
                                              color: Colors.grey,
                                              height: 24.adaptSize,
                                              width: 24.adaptSize)),
                                      prefixConstraints: BoxConstraints(maxHeight: 48.v),
                                      validator: (value) {
                                        if (value == null || value.isEmpty) {
                                          return 'هذه الخانة الزامية!';
                                        }
                                        return null;
                                      },
                                      obscureText: false,
                                      contentPadding: EdgeInsets.only(
                                          top: 15.v, right: 30.h, bottom: 15.v)),
                                  SizedBox(height: 10.v),
                                  // from to Time
                                  Row(
                                    children: [
                                      Expanded(
                                        flex: 2,
                                        child: CustomTextFormField(
                                          controller: cubit.fromTimeController,
                                          onFieldSubmitted: (value){
                                            FocusScope.of(context).unfocus();
                                            FocusScope.of(context).requestFocus(cubit.toTimeFocusNode);
                                          },
                                          textInputAction: TextInputAction.next,
                                          hintText: "الساعة",
                                          title: 'يبدأ',
                                          textInputType: TextInputType.text,
                                          prefix: InkWell(
                                            onTap: (){
                                              cubit.selectDate(context, true);
                                            },
                                            child: Container(
                                              margin: EdgeInsets.fromLTRB(
                                                16.h, 12.v, 10.h, 12.v,
                                              ),
                                              child: Icon(
                                                Icons.lock_clock,
                                                color:theme.textTheme.titleLarge?.color ,
                                              ),
                                            ),
                                          ),
                                          prefixConstraints: BoxConstraints(maxHeight: 48.v),
                                          validator: (value) {
                                            if (value == null || value.isEmpty) {
                                              return 'هذه الخانة الزامية!';
                                            }
                                            if(!isValidTime(value)){
                                              return "قيمة غير صالحة(hh:mm)";
                                            }
                                            return null;
                                          },
                                          contentPadding: EdgeInsets.only(
                                            top: 15.v,
                                            right: 30.h,
                                            bottom: 15.v,
                                          ),
                                        ),
                                      ),
                                      SizedBox(width: 8.h),
                                      Expanded(
                                        flex: 2,
                                        child: CustomTextFormField(
                                          controller: cubit.toTimeController,
                                          onFieldSubmitted: (value){
                                            FocusScope.of(context).unfocus();
                                            FocusScope.of(context).requestFocus(cubit.descriptionFocusNode);
                                          },
                                          textInputAction: TextInputAction.next,
                                          hintText: "الساعة",
                                          title: 'ينتهي',
                                          textInputType: TextInputType.text,
                                          prefix: InkWell(
                                            onTap: (){
                                              cubit.selectDate(context, false);
                                            },
                                            child: Container(
                                              margin: EdgeInsets.fromLTRB(
                                                16.h, 12.v, 10.h, 12.v,
                                              ),
                                              child: Icon(
                                                Icons.lock_clock,
                                                color:theme.textTheme.titleLarge?.color ,
                                              ),
                                            ),
                                          ),
                                          prefixConstraints: BoxConstraints(maxHeight: 48.v),
                                          validator: (value) {
                                            if (value == null || value.isEmpty) {
                                              return 'هذه الخانة الزامية!';
                                            }
                                            if(!isValidTime(value)){
                                              return "قيمة غير صالحة(hh:mm)";
                                            }
                                            return null;
                                          },
                                          contentPadding: EdgeInsets.only(
                                            top: 15.v,
                                            right: 30.h,
                                            bottom: 15.v,
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                  SizedBox(height: 10.v),
                                  // Description
                                  CustomTextFormField(
                                    controller: cubit.descriptionController,
                                    focusNode:cubit.descriptionFocusNode,
                                    onFieldSubmitted: (value){
                                      FocusScope.of(context).unfocus();
                                    },
                                    maxLines: 10,
                                    textInputAction: TextInputAction.newline,
                                    hintText: "ادخل وصف مختصر عن النادي",
                                    title: 'الوصف',
                                    textInputType: TextInputType.multiline,
                                    validator: (value) {
                                      return null;
                                    },
                                    contentPadding: EdgeInsets.only(
                                      top: 15.v,
                                      right: 30.h,
                                      bottom: 15.v,
                                    ),
                                  ),
                                  SizedBox(height: 10.v),
                                ],
                              ),
                          ),
                        )),
                  ),
                  Row(
                    children: [
                      Expanded(
                        flex: 2,
                        child: CustomElevatedButton(
                            height: 45,
                            text:cubit.editMode ?"حفظ" : "إضافة",
                            buttonTextStyle: CustomTextStyles.titleSmallOnPrimaryContainer,
                            margin:const EdgeInsets.all(16),
                            onPressed: () {
                              FocusScope.of(context).unfocus();
                              if(cubit.formKey.currentState!.validate()){
                                if(cubit.editMode) {
                                  cubit.updateExercise(context);
                                } else {
                                  cubit.addExercise(context);
                                }
                              }
                              //onTapSignIn(context);
                            }),
                      ),
                      Expanded(
                        flex: 2,
                        child: CustomElevatedButton(
                            height: 45,
                            text:"رجوع",
                            buttonStyle: ButtonStyle(
                            backgroundColor: MaterialStateProperty.all<Color>(Colors.redAccent), // Set the desired background color here
                            ),
                            margin:const EdgeInsets.all(16),
                            buttonTextStyle: CustomTextStyles.titleSmallOnPrimaryContainer,
                            onPressed: () {
                              Navigator.pop(context);
                            }),
                      ),
                    ],
                  ),

                ],
              ),


            );
          },
        ),
      ),
    );
  }
  PreferredSizeWidget _buildAppBar(BuildContext context) {
    return CustomAppBar(
        leading: OutlineIconButton(
          imagePath: ImageConstant.imgRightIcon,
          onTapped: (){
            Navigator.pop(context);
          },
        ),
        title: AppbarSubtitle(
            text: "إضافة تمرين", margin: EdgeInsets.only(left: 12.h)),
    );
  }
}
